"""
容器内并发处理示例
功能：演示如何在单个容器内并发处理多个请求
特点：使用 @modal.concurrent 装饰器提高吞吐量
运行：modal run 18_concurrent_processing.py
"""
import modal
import time
import asyncio

app = modal.App("concurrent-app")

# 允许每个容器最多并发处理 100 个请求
@app.function()
@modal.concurrent(max_inputs=100)
async def high_throughput_endpoint(request_id):
    await asyncio.sleep(1) # 模拟 I/O 操作
    return f"Processed request {request_id}"

@app.local_entrypoint()
def main():
    # 发送多个并发请求
    request_ids = list(range(10))
    results = list(high_throughput_endpoint.map(request_ids))
    for result in results:
        print(result)